﻿using System;
using System.Collections.Generic;
 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExtAspNet;
using System.Text;

namespace HY.WEB.OA
{
    public partial class tellist : PageBase
    {

        public string whe = "",whe2="";
        public string us,um,ubh;
        public string tab = "";
        public int pgCount = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            um = Session["user"].ToString();
            ubh = Session["bh"].ToString();
        


            if (ViewState["tab"] != null)
            {
              if (ViewState["tab"].ToString() != "")
                    tab = ViewState["tab"].ToString();         
            }else
               tab = "comp_tel";

            if (ViewState["whe"] != null)
            {
                if (ViewState["whe"].ToString() != "")
                    whe = ViewState["whe"].ToString();
            }
            else
                whe = "";

            if (ViewState["whe2"] != null)
            {
                if (ViewState["whe2"].ToString() != "")
                    whe2 = ViewState["whe2"].ToString();
            }
            else
                whe2 = "";


            pgCount = 0;
            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2);
            pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0) pgCount += 1;

            lblcount.Text = "总记录:" + Grid1.RecordCount.ToString();
          
            if (!IsPostBack)
            {
                if (tab == "") tab = "comp_tel";
                fill(); //填充查询

           //  btnGet.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
           //  btnSearch.OnClientClick =  Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");


                string person = ddlperson.SelectedText.Trim();
                if (person != "")
                {
                    whe = whe + " and pname=''" + person + "''";
                    whe2 = whe2 + " and pname='" + person + "'";
                }

                BindGrid();

            
                pageset();
               
            }

        
        }

        //页面设置
        void pageset()
        {
            Grid1.RecordCount = BLL.Bllbase.page_count(tab, whe2);

            pgCount = Grid1.RecordCount / Grid1.PageSize;
            if (Grid1.RecordCount % Grid1.PageSize != 0)
            {
                pgCount += 1;
            }
            Grid1.PageCount = pgCount;
            Grid1.PageIndex = 1;

            txtgo.Text = Grid1.PageIndex.ToString();
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            lblpgct.Text = "总页数:" + pgCount.ToString();
            lblcount.Text = "总记录:" + Grid1.RecordCount.ToString();
        }



        private void fill()
        {
            DataTable du = BLL.Bllbase.get_table("company");
            ddlcomp.Items.Clear();
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlcomp.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["cname"].ToString(), du.Rows[i]["cbh"].ToString()));
            }
            ddlcomp.DataBind();
            ddlcomp.Items.Add(new ExtAspNet.ListItem("", ""));


            du = BLL.Bllbase.get_table("sys_user");
            ddlperson.Items.Clear();
            for (int i = 0; i < du.Rows.Count; i++)
            {
                ddlperson.Items.Add(new ExtAspNet.ListItem(du.Rows[i]["user_name"].ToString(), du.Rows[i]["sbh"].ToString()));
            }
            ddlperson.DataBind();
            ddlperson.Items.Add(new ExtAspNet.ListItem("", ""));

            ddlperson.SelectedIndex = ddlperson.Items.IndexOf(ddlperson.Items.FindByValue(ubh));
            ddlcomp.SelectedIndex = ddlcomp.Items.IndexOf(ddlcomp.Items.FindByValue(""));
        }

        //最姓名
        public string getnm(string nmid)
        {
            BLL.HR.sys_user u = new BLL.HR.sys_user();
            return BLL.BLLfun.getname(nmid);
        }

        protected void btsearch_Click(object sender, EventArgs e)
        {
            string comp = ddlcomp.SelectedText.Trim();
            string person = ddlperson.SelectedText.Trim();
            string dts1 = date1.Text.Trim();
            string dts2 = date2.Text.Trim();
            string dtype = ddltype.SelectedValue.Trim();
            string key = txtcomp.Text.Trim();

            ViewState["whe2"] = "";
            ViewState["whe"] = "";

            if (comp != "")
            {
                whe = whe + " and cname like ''%" + comp + "%'' ";
                whe2 = whe2 + " and cname like '%" + comp + "%' ";
            }

            if (person != "")
            {
                whe = whe + " and pname=''" + person + "''";
                whe2 = whe2 + " and pname='" + person + "'";
            }


          if (dts1 != "" & dts2 != "")
           {
                whe = whe + " and convert(char(10),pdtime,120)>=convert(char(10),convert(datetime,''" + dts1 + "''),120) and " +
                    " convert(char(10),pdtime,120)<=convert(char(10),convert(datetime,''" + dts2 + "''),120)";

                whe2 = whe2 + " and convert(char(10),pdtime,120)>=convert(char(10),convert(datetime,'" + dts1 + "'),120) and " +
                   " convert(char(10),pdtime,120)<=convert(char(10),convert(datetime,'" + dts2 + "'),120)";
            }


            if (dtype != "")
            {
              whe = whe + " and left(cbh,2)=''" + dtype + "''";
              whe2 = whe2 + " and left(cbh,2)='" + dtype + "'";
            }


            if (key != "")
            {
              whe = whe + " and cname like ''%" + key + "%''";
              whe2 = whe2 + " and cname like '%" + key + "%'";
            }


            // DataTable dt = BLL.Bllbase.get_table(tab,whe);
            //Grid2.PageIndex = 1;

            ViewState["whe2"] = whe2;
            ViewState["whe"] = whe;

            
            BindGrid();

            pageset();

        }

       
        private void BindGrid()
        {
            
            if (tab == "") tab = "comp_tel";
            DataTable table;

      


            table = BLL.Bllbase.get_table(tab, whe, "sid desc",int.Parse(Grid1.PageIndex.ToString()), Grid1.PageSize);
       

            Grid1.DataSource = table;
            Grid1.DataBind();
            
            
        }

        //首页
        protected void btnf_Click(object sender, EventArgs e)
        {
            Grid1.PageIndex = 1;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();

        }

        //尾页
        protected void btne_Click(object sender, EventArgs e)
        {

            Grid1.PageIndex = pgCount;
            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();
            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        //上一页
        protected void btnpre_Click(object sender, EventArgs e)
        {
            if (Grid1.PageIndex > 2)
                Grid1.PageIndex = Grid1.PageIndex - 1;
            else
                Grid1.PageIndex = 1;
            lblpgid.Text = "当前页:"+Grid1.PageIndex.ToString();
            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        //下一页
        protected void btnnext_Click(object sender, EventArgs e)
        {
            if (Grid1.PageIndex < pgCount)
                Grid1.PageIndex = Grid1.PageIndex + 1;
            else
                Grid1.PageIndex = pgCount;

            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();

            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        //到指定页
        protected void btngo_Click(object sender, EventArgs e)
        {

            if (VAN.IsInt(txtgo.Text))
                Grid1.PageIndex =int.Parse(txtgo.Text);
            else
                Grid1.PageIndex = 1;

            if (int.Parse(txtgo.Text)>pgCount) Grid1.PageIndex = pgCount;
         
            if (int.Parse(txtgo.Text) <1) Grid1.PageIndex = 1;


            lblpgid.Text = "当前页:" + Grid1.PageIndex.ToString();

            BindGrid();
            txtgo.Text = Grid1.PageIndex.ToString();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        
        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            if (tab == "") tab = "comp_tel";
            DataTable table;

            table = BLL.Bllbase.get_table(tab, whe, "sid desc", int.Parse(Grid1.PageIndex.ToString()), Grid1.PageSize);   

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid1.DataSource = table;
            Grid1.DataBind();
        }

      

        //protected void Window1_Close(object sender, EventArgs e)
        //{
        //    // Alert.ShowInTop("弹出窗口关闭了！");
        //     string key = txt.Text.Trim();
        //      tab = "comp_tel";
        //      try
        //      {
        //          if (key == "") whe = BLL.pub.pubwhere.get_where_sql(us, tab);
        //          pageset();
        //          BindGrid();

        //      }catch(Exception ex)
        //      {
        //          Alert.Show("查询条件错误");
        //      }
        //      finally
        //      {
        //          whe = "";
        //      }
        //      ViewState["whe2"] = whe2;
        //      ViewState["whe"] = whe;

        //}


        ///// <summary>
        ///// 查询
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        //{
        //    txt.ShowTrigger1 = true;
        //    string ddlf = ddlfiled.SelectedValue;
        //    string ddlw = ddlwhere.SelectedValue;
        //   string key = txt.Text.Trim();
        //    if (ddlw.Equals("="))
        //    {
        //        whe = " " + ddlf + " like  ''%" + key + "%'' ";
        //        whe2 = " " + ddlf + " like  '%" + key + "%' ";
        //        }
        //    else
        //    {
        //      if (ddlf=="sid"||ddlf=="编号") 
        //          whe = "" + ddlf + " " + ddlw + " " + key + "";
        //      else
        //          whe = "" + ddlf + " " + ddlw + " '" + key + "'";

        //    }
        //    if (key == "")
        //    {
        //        whe = ""; whe2 = "";
        //    }
        //    pageset();
        //    BindGrid();
        //    ViewState["whe2"] = whe2;
        //    ViewState["whe"] = whe;
        //}

        ///// <summary>
        ///// 清除
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        //{
        //    txt.Text = String.Empty;
        //    txt.ShowTrigger1 = false;
        //    pageset();
        //    BindGrid();
        //}

      
  

        }




    
}